<?php

namespace App\Http\Services;

use App\Http\Model\AdminEquipSysConfig;
use App\Http\Model\Hint;
use App\Http\Model\Equip;
use App\Http\Model\FinanceJd;
use App\Http\Model\FinanceUserWallet;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class EquipAuctionServices extends CommonServices
{
    protected $model_finance_user_wallet;//用户钱包
    protected $jd_model;//金豆模型
    protected $admin_equip_sysconfig;//系统回收用户表

    protected $auctionChatForbiddenKey = 'EQUIP:AUCTION:CHAT:FORBIDDEN:HOUR:';//公聊封禁用户小时缓存 string EQUIP:AUCTION:CHAT:FORBIDDEN:HOUR:{USERID} => 1
    protected $auctionChatNeverKey = 'EQUIP:AUCTION:CHAT:FORBIDDEN:NEVER';//公聊封禁用户永久缓存 set EQUIP:AUCTION:CHAT:FORBIDDEN:NEVER {user_id}

    public function __construct()
    {
        $this->model_finance_user_wallet = new FinanceUserWallet();
        $this->jd_model =  new FinanceJd();
        $this->admin_equip_sysconfig = new AdminEquipSysConfig();
    }

    /**
     * 用户使用金豆购买装备
     */
    public function useJdBuyEquip($userId, $dataid, $num, $price,$jdType,$jd_desc){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        do {
            $price = $price;
            //需要金豆
            $price = $price * $num;
            
            if(!in_array($jdType,[6,7])){
                $wallet = $this->model_finance_user_wallet->get_wallet($userId);
                if(empty($wallet)){
                    $code = Hint::JD_NOT_ENOUGH;
                    break;
                }
                if( $wallet['jd_balance'] < $price ){
                    $code = Hint::JD_NOT_ENOUGH;
                    break;
                }
            }

            //数据处理
            DB::beginTransaction();
            try {
                $config = config('pay');
                //金豆
                $res = $this->jd_model->inc($userId, $dataid, $jdType, $price, $jd_desc);
                if(!$res) {
                    DB::rollBack();
                    $code = Hint::ERR_FAIL;
                    break;
                }else{
                    DB::commit(); 
                }

            } catch (\Exception $e) {
                DB::rollBack();

                write_log('error_finance', 'useJdBuyEquipAuction', ['msg'=>$e->getMessage(), 'line'=>$e->getLine()]);
                $code = Hint::ERR_FAIL;
            }

        } while (false);

        return compact('code', 'msg', 'data');
    }

    //后台-更改系统用户状态
    public function equipSyschange($id, $status){
        $code = Hint::SUCCESS;
        $msg = 'OK';
        $data = [];
        do {
            $res = $this->admin_equip_sysconfig->equipSyschange($id,$status);
            if(!$res) {
                $code = Hint::ERR_FAIL;
                $msg = '更改失败';
                break;
            }
        } while (false);

        return compact('code', 'msg', 'data');
    }

    //新增回收系统用户
    public function AddequipSysConfig($userId,$minutes){
        $code = Hint::SUCCESS;
        $msg = 'OK';
        $data = [];
        do {
            $res = $this->admin_equip_sysconfig->AddequipSysconfig($userId,$minutes);
            if(!$res) {
                $code = Hint::ERR_FAIL;
                $msg = '失败';
                break;
            }
        } while (false);

        return compact('code', 'msg', 'data');
    }

    //增加系统回收用户金豆数
    public function addEquipSysJd($userId,$dataid,$jdType,$price,$jd_desc){
        $code = Hint::SUCCESS;
        $msg = 'OK';
        $data = [];
        do {
            //数据处理
            DB::beginTransaction();
            try {
                $config = config('pay');
                //金豆
                $res = $this->jd_model->inc($userId, $dataid, $jdType, $price, $jd_desc);
                if(!$res) {
                    DB::rollBack();
                    $code = Hint::ERR_FAIL;
                    break;
                }else{
                    DB::commit(); 
                }

            } catch (\Exception $e) {
                DB::rollBack();

                write_log('error_finance', 'useJdBuyEquipAuction', ['msg'=>$e->getMessage(), 'line'=>$e->getLine()]);
                $code = Hint::ERR_FAIL;
            }
        } while (false);

        return compact('code', 'msg', 'data');
    }

    //后台增加拍卖行公聊配置
    public function chageEquipAuctionChatConfig($params){
        $param = [
            'id' => $params['id'],
            'mic_all_time' => $params['mic_all_time'],
            'min_rich_value' => $params['min_rich_value'],
            'max_rich_value' => $params['max_rich_value'],
            'min_charm_value' => $params['min_charm_value'],
            'max_charm_value' => $params['max_charm_value'],
            'min_user_rich_value' => $params['min_user_rich_value'],
            'max_user_rich_value' => $params['max_user_rich_value'],
            'min_user_recharge_value' => $params['min_user_recharge_value'],
            'max_user_recharge_value' => $params['max_user_recharge_value'],
            'after_day' => $params['after_day'],
            'status' => $params['status']
        ];
        do {
            $code = Hint::SUCCESS;
            $msg = 'OK';
            $data = [];
            //数据处理
            DB::beginTransaction();
            try {
                $res = $this->admin_equip_sysconfig->insertOrUpdate($param,$params['id']);
                DB::commit();
            }catch (\Exception $e) {
                DB::rollBack();
                write_log('error_equipAuctionChatConfig', 'equipAuctionChatConfig', ['msg'=>$e->getMessage(), 'line'=>$e->getLine()]);
            }
        }while(false);
        return compact('code', 'msg', 'data');
    }

    //举报处理
    public function report($request){
        //封禁
        if($request['type'] == 1){
            $params = [
                'status' => 2,//0=未处理 1=已处理 2=已封禁
                'hour' => $request['time'],//封禁时间0默认 1=1小时 2=永久 3=3小时
                'updated_at' => Carbon::now()->toDateTimeString()
            ];
            $status = $this->admin_equip_sysconfig->updateReport($request['id'],$params);
            if($status){
                $reportInfo = $this->admin_equip_sysconfig->getInfo($request['id']);
                if(in_array($request['time'],[1,3])){
                    $time = $request['time'] * 3600;
                    Redis::SETEX($this->auctionChatForbiddenKey.$reportInfo['report_userid'],$time,$request['time']);
                }else{
                    Redis::SADD($this->auctionChatNeverKey,$reportInfo['report_userid']);
                }  
            }
        }elseif($request['type'] == 2){
            //忽略
            $params = [
                'status' => 1,//0=未处理 1=已处理 2=已封禁
                'updated_at' => Carbon::now()->toDateTimeString()
            ];
            $status = $this->admin_equip_sysconfig->updateReport($request['id'],$params);
        }elseif($request['type'] == 3){
            //解禁
            $params = [
                'status' => 1,//0=未处理 1=已处理 2=已封禁
                'hour' => 0,//封禁时间0默认 1=1小时 2=永久 3=3小时
                'updated_at' => Carbon::now()->toDateTimeString()
            ];
            $status = $this->admin_equip_sysconfig->updateReport($request['id'],$params);
            $reportInfo = $this->admin_equip_sysconfig->getInfo($request['id']);
            Redis::DEL($this->auctionChatForbiddenKey.$reportInfo['report_userid']);
            Redis::SREM($this->auctionChatNeverKey,$reportInfo['report_userid']);
        }
        return true;
    }
}